// 一些系统信息模块的操作
import { search, addAdvise } from '@/api/sys'

const state = {
  searchList: {}
}

const mutations = {
  set_searchList (state, list) {
    state.searchList = list
  }
}

const actions = {
  // 搜索
  searchAll ({ commit, rootState, dispatch }, searchStr) {
    return new Promise((resolve, reject) => {
      search(searchStr).then(async res => {
        if (res.data.code === 200 && res.data.data) {
          const Contacts = res.data.data.Contacts ? res.data.data.Contacts.map(item => {
            const newItem = {
              id: item.user_id,
              fileType: 1,
              name: item.remarks ? item.remarks : item.user_name,
              words: item.remarks ? '用户：' + item.user_name : '',
              imgID: item.user_img
            }
            return newItem
          }) : []
          const oldGroups = res.data.data.Groups ? res.data.data.Groups : []
          const Groups = []
          for (let i = 0; i < oldGroups.length; i++) {
            if (!rootState.chatInformation.groupList[oldGroups[i].group_id]) {
              const reslut = await dispatch('groups/getGroupInfo', oldGroups[i].group_id, { root: true })
              commit('chatInformation/add_group', {
                groupID: oldGroups[i].group_id,
                groupInfo: { group_name: oldGroups[i].group_name, group_sponsor: reslut.data.data.group_sponsor, group_img_id: oldGroups[i].group_img }
              }, { root: true })
            }
            const newItem = {
              id: oldGroups[i].group_id,
              fileType: 6,
              name: oldGroups[i].group_name,
              imgID: oldGroups[i].group_img
            }
            Groups.push(newItem)
          }
          const judgeList = { Contacts, Groups }
          commit('set_searchList', judgeList)
        }
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  },
  // 添加意见反馈
  addAdvise (context, { adviseContent, adviseImgs, adviseType }) {
    return new Promise((resolve, reject) => {
      addAdvise(adviseContent, adviseImgs, adviseType).then(res => {
        resolve(res)
      }).catch(err => {
        reject(err)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
